/**
 * Copyright 2014 @ z3q.net.
 * name :
 * author : jarryliu
 * date : 2013-12-12 16:52
 * description :
 * history :
 */

package sale

import (
    "dhfshop/models"
	"strings"
	"errors"
	"time"
	"github.com/astaxie/beego/orm"
)

//产品线
type ProductCat struct {
   Id           int64     `orm:"column(id);pk;auto" json:"id" form:"id"`                        //产品线编号
   Name         string    `orm:"column(name)" json:"name" form:"name"`                          //产品线名称。默认产品线
   ProductNum   int       `orm:"column(product_num)" json:"product_num" form:"product_num"`     //产品线中产品数量
   Ordinary     float32   `orm:"column(ordinary);digits(12);decimals(2)" json:"ordinary" form:"ordinary"` /*普通会员折扣*/
   Bronze       float32   `orm:"column(bronze);digits(12);decimals(2)" json:"bronze" form:"bronze"`       /*铜牌会员折扣*/
   Silver       float32   `orm:"column(silver);digits(12);decimals(2)" json:"silver" form:"silver"`      /*银牌会员折扣*/
   Gold         float32   `orm:"column(gold);digits(12);decimals(2)" json:"gold" form:"gold"`             /*金牌会员折扣*/
   Platinum     float32   `orm:"column(platinum);digits(12);decimals(2)" json:"platinum" form:"platinum"` /*白金会员折扣*/
   CreatedAt    time.Time `orm:"auto_now_add;column(created_at);type(datetime)" json:"-" form:"-"`       /* 创建时间折扣 */
   CreatedAtStr string    `orm:"-" json:"created_at" form:"-"`
   UpdatedAt    time.Time `orm:"auto_now;column(updated_at);type(datetime)" json:"-" form:"-"` 
   UpdatedAtStr string    `orm:"-" json:"updated_at" form:"-"`
}

func init() {
    orm.RegisterModel(new(ProductCat))   
}

func AddProductCat(m *ProductCat) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func GetProductCatById(id int64) (v *ProductCat, err error) {
	o := models.GetDefaultDB()
	v = &ProductCat{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetAllProductCats(query map[string]string, fields []string, order_by map[string]string, 
    offset int, limit int) ([]*ProductCat, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(ProductCat))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*ProductCat
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		return lists, cnt, nil
	}
	return nil, 0, err
}

func UpdateProductCatById(m *ProductCat, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := ProductCat{Id: m.Id}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}

func DeleteProductCat(id int64) (err error) {
	o := models.GetDefaultDB()
	v := ProductCat{Id: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}
